package com.sn.springboot_netty.utils;

import com.jcraft.jsch.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;

/**
 * @Author fyy
 * @Description
 * @Date 2020/6/2 14:46
 */
public class FtpFileUtil {


    private static Logger logger = LoggerFactory.getLogger(FtpFileUtil.class);
    private String url;
    private int port;
    private String username;
    private String password;
    private ChannelSftp sftp = null;
    private Session sshSession = null;

    public FtpFileUtil(String url,int port,String username,String password){
        this.username = username;
        this.password = password;
        this.port = port;
        this.url = url;
    }

    public ChannelSftp connect(){
        logger.info("sftp开始连接>>>>>>>>"+ url +">>>>>>"+ port +">>>>>>>>");
        JSch jSch = new JSch();

        try {
            jSch.getSession(username,url,port);
            sshSession = jSch.getSession(username,url,port);
            sshSession.setPassword(password);
            Properties properties = new Properties();
            properties.put("StrictHostKeyChecking","no");
            sshSession.setConfig(properties);
            sshSession.connect();
            Channel channel = sshSession.openChannel("sftp");
            channel.connect();
            sftp = (ChannelSftp) channel;

            logger.info("sftp开始连接成功>>>>>>>>"+ url +">>>>>>"+ port +">>>>>>>>");
        } catch (JSchException e) {
            logger.error("sftp连接失败：{}",e.getMessage());
            e.printStackTrace();
        }

        return sftp;
    }

    public void downFile(String localFilePath,String remoteFilePath){
        connect();
        logger.info("开始下载文件>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        String rDir = remoteFilePath; //远程路径
        String remoteFileName = "8048698C_20200702090816.jpg"; //远程文件名

        if(localFilePath.endsWith(File.separator)) {
            localFilePath = localFilePath + (localFilePath.endsWith(File.separator) ? remoteFileName : "/" + remoteFileName);
        }
        System.out.println("下载文件地址："+localFilePath);
        File file = null;
        OutputStream output = null;
        try {
            file = new File(localFilePath);
            if(file.exists()){
                file.delete();
            }
            file.createNewFile();
            sftp.cd(rDir);
            output = new FileOutputStream(file);
            sftp.get(remoteFileName, output);
            logger.info("DownloadFile:" + remoteFileName + "成功！");
        } catch (IOException | SftpException e) {
            e.printStackTrace();
        } finally {
            try {
                if(output != null)output.close();
            } catch (IOException e) {
                logger.error("文件下载流关闭异常：{}",e.getMessage());
            }
        }
        disconnect();
    }

    public void disconnect(){
        if(this.sftp != null){
            if(this.sftp.isConnected()){
                this.sftp.disconnect();
                this.sftp = null;
                logger.info("sftp连接已关闭");
            }
        }
        if(this.sshSession != null){
            if(this.sshSession.isConnected()){
                this.sshSession.disconnect();
                this.sshSession = null;
                logger.info("sshSession连接已关闭");
            }
        }
    }

    public static void main(String[] args) {
        FtpFileUtil ftp = new FtpFileUtil("39.101.190.227",22,"sftp_zhxl","zhxl@2020");
        ftp.downFile("F:\\tupian\\","/home/sftpdir/visu/patrol/");
    }


}
